package lero.service.impl;

import lero.common.MD5Util;
import lero.mapper.AdminMapper;
import lero.pojo.Admin;
import lero.service.AdminServiceI;
import lero.vo.AdminVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/4.
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminServiceI {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void insert(Admin admin) {
        if(admin.getStatus()==null ||admin.getStatus().equals("")){
            admin.setStatus("1");
        }
        String  salt="lero";
        MD5Util encoderMd5 = new MD5Util(salt, "MD5");
        String password = encoderMd5.encode("666666");
       // admin.setCreateTime(new Date());
        admin.setPassword(password);
        adminMapper.insert(admin);
    }

    @Override
    public List<Admin> select(AdminVo vo) {

        return adminMapper.select(vo);
    }

    @Override
    public Admin selectOne(int id) {
        return adminMapper.selectOne(id);
    }

    @Override
    public void delete(int id) {
        adminMapper.delete(id);
    }

    @Override
    public void deleteMany(int[] ids) {
        adminMapper.deleteMany(ids);
    }

    @Override
    public void changeStatus(@Param("id") int id, @Param("status") String status) {
        status =status.equals("on")?"0":"1";
        adminMapper.changeStatus(id,status);
    }

    @Override
    public void update(Admin admin) {
        if(StringUtils.isEmpty(admin.getStatus())){
           admin.setStatus("1");
        }else{
            admin.setStatus("0");
        }
        //admin.setStatus(StringUtils.isEmpty(admin.getStatus())?"1":"0");
        adminMapper.update(admin);
    }

    @Override
    public Admin login(Admin admin) {
        String  salt="lero";
        MD5Util encoderMd5 = new MD5Util(salt, "MD5");
        String password = encoderMd5.encode(admin.getPassword());
        admin.setPassword(password);
        return adminMapper.login(admin);
    }
}